﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using EDMDesigner.Designer.Controls.CSDLType;
using EDMDesigner.Designer.Controls.Util;
using EDMDesigner.UIBusiness;
using EDMDesigner.UIBusiness.CSDL.Type;

namespace EDMDesigner.Designer.Controls
{
    public class DesignerCanvasPreview : ContentControl
    {
        private DesignerCanvas _content;
        private ListView _typebaseDesignerListView;
        private int _zoom;

        public DesignerCanvasPreview()
        {
            if (DesignerCanvasPreviewCreated != null)
                DesignerCanvasPreviewCreated(this);
        }

        internal static EDMView EDMView { get; set; }
        internal static IUIType UIType { get; set; }

        public DesignerView DesignerView
        {
            get { return (DesignerView)GetValue(DesignerViewProperty); }
            set { SetValue(DesignerViewProperty, value); }
        }
        public static readonly DependencyProperty DesignerViewProperty =
            DependencyProperty.Register("DesignerView", typeof(DesignerView), typeof(DesignerCanvasPreview), new UIPropertyMetadata(null, (sender, e) =>
                {
                    var designerCanvasPreview = (DesignerCanvasPreview)sender;
                    designerCanvasPreview.Content = DesignerCanvas.GetDesignerCanvas(EDMView, (DesignerView)e.NewValue);
                }));

        internal new DesignerCanvas Content
        {
            get 
            {
                if (_content == null)
                    _content = (DesignerCanvas)base.Content;
                return _content; 
            }
            set 
            {
                if (value == null)
                {
                    Content.Zoom = _zoom;
                    Content.OnSelectionChanged();
                    TypebaseDesignerListView.Background = Brushes.White;
                }
                else
                {
                    value.UnselectAllTypes();
                    value.TypesVisibles.First(t => t.UIType == UIType).IsSelected = true;
                    RoutedEventHandler loadHandler = null;
                    loadHandler = delegate
                    {
                        TypebaseDesignerListView.Background = Brushes.Yellow;
                        _zoom = Content.Zoom;
                        var widthZoom = (int)(Width * 100.0 / _content.WidthNeed);
                        var heightZoom = (int)(Height * 100.0 / _content.HeightNeed);
                        _content.Zoom = Math.Min(widthZoom, heightZoom);
                        value.Loaded -= loadHandler;
                    };
                    value.Loaded += loadHandler;
                }
                _content = value;
                base.Content = value;
            }
        }

        private ListView TypebaseDesignerListView
        {
            get
            {
                if (_typebaseDesignerListView == null)
                    _typebaseDesignerListView = VisualTreeHelperUtil.GetControlsDecendant<ListView>(Content.Children.OfType<TypeBaseDesigner>().Where(tbd => tbd.UIType.BusinessInstance == UIType.BusinessInstance).First()).First();
                return _typebaseDesignerListView;
            }
        }

        public static event Action<DesignerCanvasPreview> DesignerCanvasPreviewCreated;
    }
}
